package viz_main;

import java.awt.Color;

import processing.core.PApplet;
import view.V_DisplayObject;
/*
 * The BarComponent is a visual representation of a bar in our system.
 * Right now it doesn't need much functionality but later on we may want it to be very dynamic and probably animate 
 * 
 * TODO: (Animation) when we change the bar view from one axis to another, only add/remove bars that are greater or less
 * than the total number of new bins, then animate the bars rising and falling to adjust to the new set of values.
 * 
 * TODO: (for Lanz) we may want to move some of the class hierarchy around because probably every type of VIZcomponent will have
 * the ability to grey itself out, or highlight itself. So there may be a need for a new class.
 */
public class BarComponent extends V_DisplayObject { 
	/* Any component that has a width/height, x/y, color, and can be displayed in our system will typically extend 
	 * V_DisplayObject. 
	 * 
	 * Please refer to that class in the view package to get a sense of the methods and variables it provides.
	 * Mostly these are a bunch of useful utilities for getting and setting different properties or multiple properties at once.
	 */
	public static final float BASE_ALPHA = 0.65f;
	public static final float FADE_ALPHA = 0.1f;
	
	private boolean selected = false;
	private String name;
	private float opacity = 0.8f;
	private float offX;
	private float offY;
	private float reducedHeight;
	private float reducedYLoc;
	private int originalValue;
	private int reducedValue; // reduced value is used to keep track of the total number of reduced elements
	
	// Constructor: 
	// for now you'll probably want to instantiate a new array of bars whenever the x-axis changes
	// later on we'll do what i mentioned in the TODO and we'll have to handle it a bit more delicately
	public BarComponent(float xVal, float yVal, float wt , float ht, int c, int amount)// parameters will probably be x, y, width, height and color?
	{
		init(xVal, yVal- ht, wt, ht); //offset the yPos to draw a rectangle from the top left corner
		this.setBGColor(c);
		this.visible = true;
		reducedHeight = ySize;
		reducedYLoc = yLoc;
		originalValue = amount;
		reducedValue = amount;
	}
	
	public void setName(String nm) {
		name = nm;
	}
		  
	public void setSize(float val) {
		xSize = val;
		ySize = val;
	}
		  
	// Update method for displaying this bar:
	  public void display(PApplet p, float percent, float totalHeight, float zero) 
	  {
	    if(visible)
	    {
	      float inversePercent = 1.0f-percent;	
	    	
	      if(selected) {
	    	  p.fill(50,220,210,220);
		  }
	      else {
	    	  p.fill(bgColor >> 16 & 0xFF, bgColor >> 8 & 0xFF, bgColor & 0xFF, (bgColor >> 24 & 0xFF)*FADE_ALPHA);
	      }
	      p.noStroke();
	      
	      // draw the first rectangle transparently:
	      float spacePercent0 = ySize/totalHeight;
	      float barHeight0 = Math.min(totalHeight, totalHeight * (spacePercent0/percent));
	      p.rect(xLoc, zero - barHeight0, xSize, barHeight0);
	      //p.rect(xLoc, yLoc, xSize, ySize);
	      // change the color if the bar is selected:
	      if(selected) {
	    	  p.fill(50,220,210,220);
		  }
	      else {
	    	  p.fill(bgColor >> 16 & 0xFF, bgColor >> 8 & 0xFF, bgColor & 0xFF, (bgColor >> 24 & 0xFF)*BASE_ALPHA);
	      }
	      // draw the second rectangle more opaque.
	      // when the reduced size < ySize, we'll get a double image with one bar being shorter
	      // also, when the inversePercent is used to scale the height back up if scaleToggle is enabled.
	      float spacePercent = reducedHeight/totalHeight;
	      float barHeight = totalHeight * (spacePercent/percent);
	      p.rect(xLoc, zero - barHeight, xSize, barHeight);
	    }
	  }
	  
	  public void setReducedValue(int val)
	  {
		  reducedValue = val;
	  }
	  
	  public int getReducedAmount()
	  {
		  return reducedValue;
	  }
	  
	  public void setReducedHeight(float val)
	  {
		  reducedYLoc = yLoc + (ySize - val);
		  reducedHeight = val;
	  }
	  
	  public void resetHeight()
	  {
		  reducedYLoc = yLoc;
		  reducedHeight = ySize;
		  reducedValue = originalValue;
	  }
	  
	  public void setSelected(boolean val)
	  {
	    selected = val;
	  }
	  
	  public boolean isSelected()
	  {
	    return selected;
	  }
	  
	  public void setOpacity(float val)
	  {
		  opacity = val;
	  }
}
